package com.xielx.mynovel.slice;

import com.xielx.mynovel.ResourceTable;
import com.xielx.mynovel.client.QiMaoClient;
import com.xielx.mynovel.tab.BottomTab;
import com.xielx.mynovel.tab.impl.BookShopTab;
import com.xielx.mynovel.tab.impl.BookrackTab;
import com.xielx.mynovel.ui.SearchRunable.SearchRunable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Window;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private final static HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0X10, "MainAbilitySlice");
    private TextField searchText;

    @Override
    public void onStart(Intent intent) {
        setHeader();
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initTopbar();
        initBottomTab();
    }

    private void initTopbar() {
        searchText = (TextField) findComponentById(ResourceTable.Id_search_content);
        Button serachBtn = (Button) findComponentById(ResourceTable.Id_btn_search);
        serachBtn.setClickedListener(component -> {
            try {
                String uri = "/search/index/?keyword=" + URLEncoder.encode(searchText.getText(), "UTF-8");
                QiMaoClient.doGet(this, uri,response -> {
                    Document document = Jsoup.parse(response.getStrData());
                    Elements els = document.getElementsByClass("search-no-data");
                    if (els.size()>0) {
                        getUITaskDispatcher().asyncDispatch(()->{
                            DependentLayout layout = (DependentLayout) findComponentById(ResourceTable.Id_body_content);
                            layout.removeAllComponents();
                            Text text =  new Text(this);
                            text.setTextSize(20, Text.TextSizeType.FP);
                            text.setMarginLeft(60);
                            text.setText("未查询到\""+searchText.getText()+"\"相关的小说。");
                            layout.addComponent(text);
                            //ImageSource is = ImageSource.create()

                        });
                    }else {
                        getUITaskDispatcher().asyncDispatch(new SearchRunable(searchText.getText(),uri,this));
                    }
                });
            }catch (Exception e){
                e.printStackTrace();
            }
        });
    }

    private void initBottomTab() {
        try {
            DirectionalLayout bottomTab = (DirectionalLayout) findComponentById(ResourceTable.Id_bottomBar);
            List<BottomTab> list = BottomTab.getInstances(BookrackTab.class, BookShopTab.class);
            int index = 0;
            List<DirectionalLayout> tabList = new ArrayList<>();
            Color normalColor = new Color(getResourceManager().getElement(ResourceTable.Color_colorTabTextNormal).getColor());
            Color selectColor = new Color(getResourceManager().getElement(ResourceTable.Color_colorTabTextSelected).getColor());
            for (BottomTab tempTab : list) {
                DirectionalLayout tab = (DirectionalLayout) LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_tab, bottomTab, false);
                Image buttonImage = (Image) tab.findComponentById(ResourceTable.Id_bottom_tab_button_image);
                buttonImage.setScaleMode(Image.ScaleMode.STRETCH);
                Element element = null;
                Text buttonText = (Text) tab.findComponentById(ResourceTable.Id_bottom_tab_button_text);
                buttonText.setText(getString(tempTab.getText()));
                if (0 == index) {
                    buttonText.setTextColor(selectColor);
                    element = ElementScatter.getInstance(this).parse(tempTab.getImage2());
                    index++;
                } else {
                    element = ElementScatter.getInstance(this).parse(tempTab.getImage());
                }
                buttonImage.setImageElement(element);
                tab.setClickedListener(componet -> {
                    tabList.forEach(temp -> {
                        Image big = (Image) temp.findComponentById(ResourceTable.Id_bottom_tab_button_image);
                        big.setImageElement(ElementScatter.getInstance(this).parse(((BottomTab) temp.getTag()).getImage()));
                        ((Text) temp.findComponentById(ResourceTable.Id_bottom_tab_button_text)).setTextColor(normalColor);
                    });
                    Image big = (Image) componet.findComponentById(ResourceTable.Id_bottom_tab_button_image);
                    big.setImageElement(ElementScatter.getInstance(this).parse(((BottomTab) componet.getTag()).getImage2()));
                    ((Text) componet.findComponentById(ResourceTable.Id_bottom_tab_button_text)).setTextColor(selectColor);
                });
                tab.setTag(tempTab);
                tabList.add(tab);
                bottomTab.addComponent(tab);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setHeader() {
        try {
            //修改顶部的颜色与内容体一致
            Window window = getWindow();
            window.setStatusBarColor(getResourceManager().getElement(ResourceTable.Color_headerAndBottom).getColor());
            window.setStatusBarVisibility(Component.VISIBLE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


}
